var isWx=false;
/*wx.miniProgram.getEnv(function(res) {
    if(res.miniprogram){
        //小程序
        isWx=true;
    }
});*/
/**
 * 首页
 */
function goHomePage(){
    window.location.href="/phone/index";
}
/**
 * 服务中心
 */
function goProduct(){
    window.location.href="/phone/product";
}
/**
 * 产品详情
 */
function goProductDesc(id){
    window.location.href="/phone/product/"+id;
}
/**
 * 联系我们
 */
function goLinkMe(){
    window.location.href="/phone/linkMe";
}
/**
 * 资讯
 */
function goNews(){
    window.location.href="/phone/news";
}
/**
 * 资讯列表
 */
function goNewsList(id){
    window.location.href="/phone/newsCatologe/"+id;
}
/**
 * 资讯详情
 */
function goNewsDesc(id){
    window.location.href="/phone/news/"+id;
}
/**
 * 个人中心
 */
function myHome(id){
    window.location.href="/phone/my?memberId="+id;
}

/**
 * 个人资料
 */
function myResource(id){
    window.location.href="/phone/register?memberId="+id;
}
/**
 * 附件资料
 */
function fileList(id){
    window.location.href="/phone/fileList?memberId="+id;
}
/**
 * 预约服务页面
 */
function subPage(id){
    window.location.href="/phone/subPage?memberId="+id;
}
/**
 * 预约服务修改
 */
function webJumpSub(memberId,id){
    window.location.href="/phone/webJumpSub?memberId="+memberId+"&id="+id;
}
/**
 * 预约修改服务
 */
function webSubUpdate(id,subId,page){
    window.location.href="/phone/acceptSub?memberId="+id+"&page="+page+"&subId="+subId;
}
/**
 * 预约服务
 */
function webSub(id,page){
    window.location.href="/phone/webSub?memberId="+id+"&page="+page;
}
/**
 * 预约列表
 */
function webSubList(id){
    window.location.href="/phone/subList?memberId="+id;
}
/**
 * 预约详情
 */
function webSubDesc(memberId,id){
    window.location.href="/phone/webSubDesc?memberId="+memberId+"&id="+id;
}
function goCartPage(memberId){
    window.location.href="/phone/cart?memberId="+memberId;
}
function goAddress(memberId){
    if(isWx){
        wx.miniProgram.navigateTo({url: '/pages/show/show?url=/phone/address&urlParam=memberId*'+memberId})
    }else{
        window.location.href="/phone/address?memberId="+memberId;
    }
}
function goAddressAndType(memberId,orderId,type){
    if(type=="2"){
        window.location.href="/phone/address?memberId="+memberId+"&orderId="+orderId+"&type="+type;
    }else if(type=="3"){
        window.location.href="/phone/address?memberId="+memberId+"&cartJson="+orderId+"&type="+type;
    }

}
function goDetail(memberId){
    window.location.href="/phone/detail?memberId="+memberId;
}
function goDetailOrder(memberId,orderId){
    if(isWx){
        wx.miniProgram.navigateTo({url: '/pages/show/show?url=/phone/detail&urlParam=memberId*'+memberId+'#orderId*'+orderId})
    }else{
        window.location.href="/phone/detail?memberId="+memberId+"&orderId="+orderId;
    }
}

function goActivityPage(brandId,memberId){
    if(isWx){
        wx.miniProgram.navigateTo({url: '/pages/show/show?url=/phone/activity&urlParam=memberId*'+memberId+'#brandId*'+brandId})
    }else{
        window.location.href="/phone/activity?memberId="+memberId+"&brandId="+brandId;
    }

}
function goProductPage(itemId,memberId){
    if(isWx){
        wx.miniProgram.navigateTo({url: '/pages/show/show?url=/phone/product/'+itemId+'&urlParam=memberId*'+memberId})
    }else{
        window.location.href="/phone/product/"+itemId+"?memberId="+memberId;
    }
}
function goAddAddress(memberId){
    if(isWx){
        wx.miniProgram.redirectTo({url: '/pages/show/show?url=/phone/addAddress&urlParam=memberId*'+memberId});
    }else{
        window.location.href="/phone/addAddress?memberId="+memberId;
    }
}

function goAddAddressOne(memberId,selectType,orderId){
    if(isWx){
        wx.miniProgram.redirectTo({url: '/pages/show/show?url=/phone/address&urlParam=memberId*'+memberId+'#type*'+selectType+'#orderId*'+orderId});
    }else{
        if(selectType=="2"){
            window.location.href="/phone/address?memberId="+memberId+"&type="+selectType+"&orderId="+orderId;
        }
    }
}
function goDetailAll(memberId){
    if(isWx){
        wx.miniProgram.navigateTo({url: '/pages/show/show?url=/phone/detailList&urlParam=memberId*'+memberId})
    }else{
        window.location.href="/phone/detailList?memberId="+memberId;
    }
}
function shouHou(memberId){
    if(isWx){
        wx.miniProgram.navigateTo({url: '/pages/show/show?url=/phone/shouHou&urlParam=memberId*'+memberId})
    }else{
        window.location.href="/phone/shouHou?memberId="+memberId
    }
}
function goKeFu(memberId){
    if(isWx){
        wx.miniProgram.navigateTo({url: '/pages/show/show?url=/phone/kefu&urlParam=memberId*'+memberId})
    }else{
        window.location.href="/phone/kefu?memberId="+memberId
    }

}
function goDetailList(memberId,status){
    if(isWx){
        wx.miniProgram.navigateTo({url: '/pages/show/show?url=/phone/detailList&urlParam=memberId*'+memberId+'#status*'+status})
    }else{
        window.location.href="/phone/detailList?memberId="+memberId+"&status="+status;
    }
}
function goAddAddressOrder(orderId,memberId,type){
    window.location.href="/phone/addAddress?memberId="+memberId+"&orderId="+orderId+"&type="+type;
}

function myUser(memberId){
    if(isWx){
        wx.miniProgram.navigateTo({url: '/pages/show/show?url=/phone/myUser&urlParam=memberId*'+memberId})
    }else{
        window.location.href="/phone/myUser?memberId="+memberId;
    }

}
function coupon(memberId){
    if(isWx){
        wx.miniProgram.navigateTo({url: '/pages/show/show?url=/phone/couponList&urlParam=memberId*'+memberId})
    }else{
        window.location.href="/phone/couponList?memberId="+memberId;
    }
}
function tixian(memberId){
    if(isWx){
        wx.miniProgram.navigateTo({url: '/pages/show/show?url=/phone/tixian&urlParam=memberId*'+memberId})
    }else{
        window.location.href="/phone/tixian?memberId="+memberId;
    }
}
function tixianList(memberId){
    if(isWx){
        wx.miniProgram.navigateTo({url: '/pages/show/show?url=/phone/tixianList&urlParam=memberId*'+memberId})
    }else{
        window.location.href="/phone/tixianList?memberId="+memberId;
    }
}
function logList(memberId,type){
    if(type==null||type==""){
        type='9';
    }
    if(isWx){
        wx.miniProgram.navigateTo({url: '/pages/show/show?url=/phone/logList&urlParam=memberId*'+memberId+'#type*'+type})
    }else{
        window.location.href="/phone/logList?memberId="+memberId+"&type="+type;
    }
}

function goAddAddressById(addressId,memberId){
    window.location.href="/phone/addAddress?memberId="+memberId+"&addressId="+addressId;
}

function goAddAddressOrderById(orderId,addressId,memberId,type){
    window.location.href="/phone/addAddress?memberId="+memberId+"&addressId="+addressId+"&orderId="+orderId+"&type="+type;
}

function goDetailAndAddressId(orderId,addressId,memberId,type){
    if(isWx){
        wx.miniProgram.redirectTo({url: '/pages/show/show?url=/phone/detail&urlParam=memberId*'+memberId+'#addressId*'+addressId+'#orderId*'+orderId+'#type*'+type})
    }else{
        if(type=="2"){
            window.location.href="/phone/detail?memberId="+memberId+"&addressId="+addressId+"&orderId="+orderId+"&type="+type;
        }else if(type=="3"){
            //window.location.href="/phone/detail?memberId="+memberId+"&addressId="+addressId+"&orderId="+orderId+"&type="+type;
            window.location.href="/phone/orderDetail?memberId="+memberId+"&cartJson="+orderId+"&addressId="+addressId;
        }
    }
}

function productPutCart(obj,activityId,memberId,itemId,objClass){
    cart(obj);
    $.post("/phone/productPutCart",{
        memberId : memberId,
        activityId : activityId,
        itemId:itemId,
        type:"2",
        buyNum:1
    },function(resultInfo){
        console.log(resultInfo);
        if (resultInfo.code == 0){
            $("."+objClass).html(resultInfo.msg);
        }else{
            $.toast("商品添加购物车失败！");
        }
    },"json");
}

function sharePage(memberId){
    if(isWx){
        wx.miniProgram.navigateTo({url: '/pages/show/show?url=/phone/share&urlParam=memberId*'+memberId})
    }else{
        window.location.href="/phone/share?memberId="+memberId;
    }
}

function share(unionid){
    $.showLoading("正在制作图片中");
    $.ajax({
        type: "POST",
        url: "/phone/acceptSharePicByUnionid/"+unionid,
        success: function(result){
            $.hideLoading();
            if(1==result.returnInfo.state){
                wx.miniProgram.navigateTo({url: '/pages/share/share?url='+result.returnInfo.result+"&params="+result.returnInfo.errorMsg})
            }else{
                $.toast(result.returnInfo.errorMsg, "text");
            }
        },
        error: function(result){
            $.hideLoading();
            $.toast(result.returnInfo.errorMsg, "text");
        }
    });
}

function shareProduct(unionid,itemId,activityId){
    $.showLoading("正在制作图片中");
    $.ajax({
        type: "POST",
        url: "/phone/acceptShareProductByUnionid/"+unionid,
        data: {"itemId":itemId,"activityId":activityId},
        success: function(result){
            $.hideLoading();
            if(1==result.returnInfo.state){
                wx.miniProgram.navigateTo({url: '/pages/share/share?url='+result.returnInfo.result+"&params="+result.returnInfo.errorMsg})
            }else{
                $.toast(result.returnInfo.errorMsg, "text");
            }
        },
        error: function(result){
            $.hideLoading();
            $.toast(result.returnInfo.errorMsg, "text");
        }
    });
}

function shareBrand(unionid,activityId){
    $.showLoading("正在制作图片中");
    $.ajax({
        type: "POST",
        url: "/phone/acceptShareBrandByUnionid/"+unionid,
        data: {"activityId":activityId},
        success: function(result){
            $.hideLoading();
            if(1==result.returnInfo.state){
                wx.miniProgram.navigateTo({url: '/pages/share/share?url='+result.returnInfo.result+"&params="+result.returnInfo.errorMsg})
            }else{
                $.toast(result.returnInfo.errorMsg, "text");
            }
        },
        error: function(result){
            $.hideLoading();
            $.toast(result.returnInfo.errorMsg, "text");
        }
    });
}

//支付
function payWx(money,openid,recordId,id) {
    wxPay(money,openid,recordId,id);
}


/**
 * @author 陈维斌 http://www.cnblogs.com/Orange-C/p/4042242.html%20
 * 如果想将日期字符串格式化,需先将其转换为日期类型Date
 * 以下是提供几种常用的
 *
 * var da = new Date().format('yyyy-MM-dd hh:mm:ss'); //将日期格式串,转换成先要的格式
 * alert("格式化日期类型 \n" + new Date() + "\n 为字符串：" + da);
 *
 * var str = "2014/01/01 01:01:01" // yyyy/mm/dd这种格式转化成日期对像可以用new Date(str);在转换成指定格式
 * alert("格式化字符串\n" + str + " 为日期格式 \n" + new Date(str).format('yyyy-MM-dd hh:mm:ss'))
 *
 *
 * var str1 = "2014-12-31 00:55:55" // yyyy-mm-dd这种格式的字符串转化成日期对象可以用new Date(Date.parse(str.replace(/-/g,"/")));
 * alert("格式化字符串\n" + str1 + " 为日期格式 \n" + new Date(Date.parse(str1.replace(/-/g, "/"))).format('yyyy-MM-dd hh:mm:ss'))
 *
 *
 * 日期加月
 * 先将字符转换成Date类型才可以使用
 * var str1 = "2014-12-31 00:55:55" // yyyy-mm-dd这种格式的字符串转化成日期对象可以用new Date(Date.parse(str.replace(/-/g,"/")));
 * 例如 var saveDate = new Date(Date.parse(str1.replace(/-/g, "/"))).addMonth(5)
 * addMonth(月数) 必须为整数
 */

Date.prototype.format = function (format) {
    var date = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S+": this.getMilliseconds()
    };
    if (/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in date) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1
                ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
        }
    }
    return format;
}
Date.daysInMonth = function (year, month) {
    if (month == 1) {
        if (year % 4 == 0 && year % 100 != 0)
            return 29;
        else
            return 28;
    } else if ((month <= 6 && month % 2 == 0) || (month = 6 && month % 2 == 1))
        return 31;
    else
        return 30;
};
Date.prototype.addMonth = function (addMonth) {
    var y = this.getFullYear();
    var m = this.getMonth();
    var nextY = y;
    var nextM = m;
    //如果当前月+要加上的月>11 这里之所以用11是因为 js的月份从0开始
    if (m > 11) {
        nextY = y + 1;
        nextM = parseInt(m + addMonth) - 11;
    } else {
        nextM = this.getMonth() + addMonth
    }
    var daysInNextMonth = Date.daysInMonth(nextY, nextM);
    var day = this.getDate();
    if (day > daysInNextMonth) {
        day = daysInNextMonth;
    }
    return new Date(nextY, nextM, day);
};
